package com.cogent.dynamicform.bus.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.cogent.business.helper.WorkFolwHelper;
import com.cogent.common.date.JodaDateUtil;
import com.cogent.common.util.ClassReflectUtil;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.facade.BussinessDaoFacade;
import com.cogent.dynamicform.bus.DynamicFormBussiness;
import com.cogent.dynamicform.bus.IDynamicFormAfterBussiness;
import com.cogent.dynamicform.bus.IDynamicFormBeforeBussiness;
import com.cogent.dynamicform.bus.IDynamicFormCustomBussiness;
import com.cogent.dynamicform.domain.BusControlDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.domain.FormDomain;
import com.cogent.dynamicform.domain.FormQueryDomain;
import com.cogent.dynamicform.facade.IExpressionFacade;
import com.cogent.dynamicform.facade.impl.ExpressionFacadeImpl;
import com.cogent.dynamicform.util.DynamicFormBusUtil;
import com.cogent.expression.facade.ExpressionFacade;
import com.cogent.validator.domain.ValidatorDomain;
import com.cogent.validator.facade.ValidatorFacade;
import com.cogent.validator.facade.impl.ValidatorFacadeImpl;

public class DynamicFormBussinessImpl implements DynamicFormBussiness {
	
	public void getAddValue(FormDomain formDomain, HashMap formMap) {
		List columnList = formDomain.getColumnList();
		Iterator it = columnList.iterator();
		while(it.hasNext()) {
			FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
			String curvalue = ExpressionFacade.getExpressionValue(formMap, formColumnDomain.getCalformula());
			if(StringUtil.isEmpty(curvalue))
				curvalue = formColumnDomain.getDefaultvalue();
			if(!StringUtil.isEmpty(curvalue))
				formMap.put(formColumnDomain.getName(), curvalue);
		}
	}
	
	public List getQueryList(FormDomain formDomain, HashMap formMap) {
		List queryList = new ArrayList();
		List queryColumnList = formDomain.getQueryList();
		Iterator it = queryColumnList.iterator();
		String districtcode = MapUtil.getStringValue(formMap, "districtcode");		
		boolean isdistrict = false;
		while(it.hasNext()) {
			FormQueryDomain formQueryDomain = (FormQueryDomain)it.next();
			if(!StringUtil.isEmpty(formQueryDomain.getCountexpression()))
				ExpressionFacade.getFormValue(formMap, formQueryDomain.getName(), formQueryDomain.getCountexpression());
			String curvalue = ExpressionFacade.getExpressionValue(formMap, formQueryDomain.getCalformula());
			if(StringUtil.isEmpty(curvalue))
				curvalue = MapUtil.getStringValue(formMap, formQueryDomain.getName(), ExpressionFacade.getExpressionValue(formMap, formQueryDomain.getExpression()));
			if(StringUtil.isEmpty(formQueryDomain.getTablename()))
				queryList.add(new QueryParamDomain(formQueryDomain.getName(), curvalue, formQueryDomain.getQuerytype()));
			else
				queryList.add(new QueryParamDomain(formQueryDomain.getTablename(), formQueryDomain.getName(), formQueryDomain.getMapedFileld(),
						curvalue, formQueryDomain.getQuerytype()));
		}
		
		return queryList;
	}
	
	public String getScript(FormDomain formDomain) {
		List validatorList = formDomain.getValidatorList();
		ValidatorFacade validatorFacade = new ValidatorFacadeImpl();
		List subList = new ArrayList();
		String prefix = "DYNTABLE";
		if(formDomain.getQueryDynTableDomain() != null) {
			subList = DynamicFormBusUtil.getValidatorList(formDomain.getQueryDynTableDomain().getDynTableColumnList(), "_");
			prefix = "QUERYDYNTABLE";
		} else if((formDomain.getDynTableList() != null) && (formDomain.getDynTableList().size() > 0))
			subList = DynamicFormBusUtil.getValidatorList(formDomain.getDynTableList());
		List curValidatorList = new ArrayList();
		Iterator it = validatorList.iterator();
		while(it.hasNext()) {
			ValidatorDomain validatorDomain = (ValidatorDomain)it.next();
			String keyname = validatorDomain.getProperty();
			FormColumnDomain FormColumnDomain = (FormColumnDomain)formDomain.getColumnMap().get(keyname);
			validatorDomain.setKey(FormColumnDomain.getCn());
			curValidatorList.add(validatorDomain);
		}
		return validatorFacade.getScript(curValidatorList, subList, prefix);
	}
	
	public String validator(FormDomain formDomain, HashMap dataMap) {
		List validatorList = formDomain.getValidatorList();
		List curValidatorList = new ArrayList();
		Iterator it = validatorList.iterator();
		while(it.hasNext()) {
			ValidatorDomain validatorDomain = (ValidatorDomain)it.next();
			String keyname = validatorDomain.getProperty();
			FormColumnDomain FormColumnDomain = (FormColumnDomain)formDomain.getColumnMap().get(keyname);
			validatorDomain.setKey(FormColumnDomain.getCn());
			curValidatorList.add(validatorDomain);
		}
		ValidatorFacade validatorFacade = new ValidatorFacadeImpl();
		
		return validatorFacade.validator(curValidatorList, dataMap);
	}
	
	public String dealBussiness(BussinessDaoFacade bussinessFacade, FormDomain formDomain, HashMap valMap) {
		return dealBussiness(bussinessFacade, formDomain, valMap, false);
	}
	
	public String dealBussiness(BussinessDaoFacade bussinessFacade, FormDomain formDomain, HashMap dataMap, boolean ispda) {
		String ret = "-1";
		HashMap orginMap = new HashMap();
		try{
			bussinessFacade.createConn();			
			if(formDomain.getCustomtype().equals("CUSTOM")) {
				IDynamicFormCustomBussiness custombussiness = (IDynamicFormCustomBussiness)ClassReflectUtil.getNewInstance(formDomain.getCustomclass());
				return custombussiness.dealBussiness(bussinessFacade, formDomain, dataMap, ispda);
			}
			if(formDomain.getCustomtype().equals("BEFORE")) {
				IDynamicFormBeforeBussiness beforecustombussiness = (IDynamicFormBeforeBussiness)ClassReflectUtil.getNewInstance(formDomain.getCustomclass());
				beforecustombussiness.dealBussiness(bussinessFacade, formDomain, dataMap, ispda);
			}
			try{
				bussinessFacade.beginTran();
				if(!StringUtil.isEmpty(formDomain.getMaptable())) {
					String id = MapUtil.getStringValue(dataMap, "id");
					if(StringUtil.isEmpty(id))
						ispda = false;
					if(ispda) {				
						List queryList = new ArrayList();
						queryList.add(new QueryParamDomain("id", id));
						orginMap = bussinessFacade.getByID(formDomain.getMaptable(), queryList);
					}
					String date = JodaDateUtil.getCurDate();
					if((ispda) && (orginMap.size() > 0))
						dataMap.put("modifydate", date);
					else {
						dataMap.put("credate", date);
						dataMap.put("modifydate", date);
						if(MapUtil.isEmpty(dataMap, "status")){
							dataMap.put("status", "0");
						}
					}
					if(MapUtil.getIntValue(dataMap, "wf_opstate")> 0)
						dataMap.put("status", WorkFolwHelper.getWorkFlowStatus(dataMap));
					if(ispda) {
						if(orginMap.size() > 0)
							ret = bussinessFacade.update(formDomain.getMaptable(), dataMap);
						else
							ret = bussinessFacade.save(formDomain.getMaptable(), dataMap);
					} else
						ret = bussinessFacade.saveOrUpdate(formDomain.getMaptable(), dataMap);
				}
				if((formDomain.getDynTableList() != null) && (formDomain.getDynTableList().size() > 0)) {
					if(ret.equals("-1"))
						ret = MapUtil.getStringValue(dataMap, formDomain.getDynTableParentid());
					DynamicFormBusUtil.dealDynTableBus(bussinessFacade, formDomain, dataMap, ret);
				}
				if(formDomain.getQueryDynTableDomain() != null) {
					if(ret.equals("-1"))
						ret = MapUtil.getStringValue(dataMap, formDomain.getQueryDynTableDomain().getParentid());
					DynamicFormBusUtil.dealQueryDynTableBus(bussinessFacade, formDomain, dataMap, ret);
				}
				if(formDomain.getBusControlList() != null) {
					IExpressionFacade iexpressionFacade = new ExpressionFacadeImpl();
					List list = formDomain.getBusControlList();
					Iterator it = list.iterator();
					while(it.hasNext()) {
						BusControlDomain busControlDomain = (BusControlDomain)it.next();
						iexpressionFacade.doExpression(busControlDomain, bussinessFacade, formDomain, dataMap);
					}
				}
				if((formDomain.isIsworkflow()) && (MapUtil.getIntValue(dataMap, "status") > 0))
					WorkFolwHelper.dealWorkFlowBussiness(bussinessFacade, ret, dataMap);
				if(formDomain.getCustomtype().equals("AFTER")) {
					IDynamicFormAfterBussiness aftercustombussiness = (IDynamicFormAfterBussiness)ClassReflectUtil.getNewInstance(formDomain.getCustomclass());
					aftercustombussiness.dealBussiness(bussinessFacade, formDomain, dataMap, ispda);
				}
				bussinessFacade.commitTran();
			} catch (Exception ex) {
				bussinessFacade.rollbackTran();
			}
		}finally{
			bussinessFacade.closeConn();
		}
		
		return ret;
	}
}
